package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcLabItemEntity;
import com.dhcc.cdc.busi.service.ICdcLabItemService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 检验项目
 * 
 * @ClassName: LabItemController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-26 16:22:25
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdclabitem")
public class CdcLabItemController extends BaseController {

	private String PREFIX = "/backend/cdc/labitem/";

	@Autowired
	private ICdcLabItemService labItemService;
	

    /**
     * 跳转到检验项目列表首页
     */
    @RequestMapping("")
    @Permission("labitem:list")
    public String index() {
        return PREFIX + "labitem.html";
    }
	
    /**
     * 跳转到新增检验项目
     */
    @RequestMapping("/to_add")
    @Permission("labitem:add")
    public String toAdd() {
        return PREFIX + "labitem_add.html";
    }
	
	/**
     * 跳转到修改检验项目
     */
    @RequestMapping("/to_update/{labItemId}")
    @Permission("labitem:update")
    public String toUpdate(@PathVariable Long labItemId, Model model) {
        CdcLabItemEntity labItem = this.labItemService.selectById(labItemId);
        model.addAttribute("labItem",labItem);
        return PREFIX + "labitem_edit.html";
    }

    /**
     * 获取检验项目列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("labitem:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcLabItemEntity> page = new PageFactory<CdcLabItemEntity>().defaultPage();
        List<CdcLabItemEntity> result = this.labItemService.page(page,params);
        page.setRecords((List<CdcLabItemEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增检验项目
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("labitem:add")
    @BussinessLog(value = "新增检验项目")
    public Object add(CdcLabItemEntity labItem) {
        if (CommonUtil.isOneEmpty(labItem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        labItem.insert();
        return R.ok();
    }

    /**
     * 删除检验项目
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("labitem:delete")
    @BussinessLog(value = "删除检验项目")
    public Object delete(@RequestParam Long labItemId) {
	
        this.labItemService.deleteById(labItemId);

        return R.ok();
    }

    /**
     * 修改检验项目
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("labitem:update")
    @BussinessLog(value = "修改检验项目")
    public Object update(CdcLabItemEntity labItem) {
        if (CommonUtil.isOneEmpty(labItem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcLabItemEntity old = this.labItemService.selectById(labItem.getId());

        old.setXcode(labItem.getXcode());
        old.setCode(labItem.getCode());
        old.setDescription(labItem.getDescription());
        old.setSignificance(labItem.getSignificance());
        old.setNormalValue(labItem.getNormalValue());
        old.setBusLabValueTypeId(labItem.getBusLabValueTypeId());
        old.setAbbreviation(labItem.getAbbreviation());
        old.setNote(labItem.getNote());
        old.setDataId(labItem.getDataId());
        old.setIsActive(labItem.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
